package com.example.day3.exercise;

import java.util.function.IntConsumer;

/**
 * 使用stream生成斐波那契数列的前10项
 */
public class Exercise8 {
    public static void main(String[] args) {
        fibonacci(10, System.out::println);
    }

    static int[] cache = new int[100];

    static int fibonacci(int n, IntConsumer consumer) {
        if (cache[n] > 0) {
            return cache[n];
        }
        if (n == 1 || n == 2) {
            cache[n] = 1;
            consumer.accept(1);
            return cache[n];
        }
        int r = fibonacci(n - 1, consumer) + fibonacci(n - 2, consumer);
        cache[n] = r;
        consumer.accept(r);
        return r;
    }
}
